package com.wenwei.paper.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.wenwei.paper.R;
import com.wenwei.paper.base.AtyContainer;
import com.wenwei.paper.base.BaseActivity;
import com.wenwei.paper.base.Constant;
import com.wenwei.paper.bean.VersionBean;
import com.wenwei.paper.net.ApiManager;
import com.wenwei.paper.net.BaseSubscriber;
import com.wenwei.paper.utils.RogerSPUtils;
import com.wenwei.paper.utils.VersionUtils;
import com.wenwei.paper.view.CustomDelDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/13.
 */
public class SettingActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_set;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("设置");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
    private CustomDelDialog mExitLoginDialog;
    private CustomDelDialog mVersionDialog;
    @OnClick({R.id.base_back, R.id.setting_about_rl, R.id.setting_contact_rl, R.id.setting_version_rl, R.id.setting_update_rl, R.id.user_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finish();
                break;
            case R.id.setting_about_rl:
                break;
            case R.id.setting_contact_rl:
                break;
            case R.id.setting_version_rl:
                break;
            case R.id.setting_update_rl:
                getVersion();
                break;
            case R.id.user_login:
                if(mExitLoginDialog == null){
                    mExitLoginDialog = new CustomDelDialog(mContext);
                    mExitLoginDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    mExitLoginDialog.setDialogMsg("残忍退出？");
                    mExitLoginDialog.setDelDialogEvent(new CustomDelDialog.DelDialogEvent() {
                        @Override
                        public void onDelete() {
                            mExitLoginDialog.dismiss();
                            RogerSPUtils.clear(mContext);
                            AtyContainer.getInstance().finishAllActivity();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {

                                        goActivity(mContext,LoginActivity.class);
                                    }catch (Exception e){}
                                }
                            }).start();
                        }

                        @Override
                        public void onCancel() {
                            mExitLoginDialog.dismiss();
                        }
                    });
                }
                mExitLoginDialog.show();
                break;
        }
    }


    private BaseSubscriber<VersionBean> versionSubscriber;
    private void getVersion(){
        versionSubscriber = new BaseSubscriber<VersionBean>(mContext,true,"请稍后...") {
            @Override
            public void onSuccess(final VersionBean versionBean) {
                if(Integer.parseInt(versionBean.getVersion_code()) == VersionUtils.getVersionCode(mContext)){
                    showToast("当前已是最新版本");
                }else if (Integer.parseInt(versionBean.getVersion_code()) > VersionUtils.getVersionCode(mContext)){
                    if(mVersionDialog == null){
                        mVersionDialog = new CustomDelDialog(mContext);
                        mVersionDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                        mVersionDialog.setDialogMsg("发现新版本？");
                        mVersionDialog.setDialogSubMsg(versionBean.getVersion_id());
                        mVersionDialog.setDelDialogEvent(new CustomDelDialog.DelDialogEvent() {
                            @Override
                            public void onDelete() {
                                Intent intent= new Intent();
                                intent.setAction("android.intent.action.VIEW");
                                Uri content_url = Uri.parse(Constant.BASE_IMG_URL+versionBean.getUrl());
                                intent.setData(content_url);
                                startActivity(intent);
                                mVersionDialog.dismiss();
                            }

                            @Override
                            public void onCancel() {
                                mVersionDialog.dismiss();
                            }
                        });
                    }
                    mVersionDialog.show();
                }
            }
        };
        ApiManager.getInstance().getVersion(versionSubscriber, VersionUtils.getVersionCode(mContext));
    }
}
